<template>
  <div class="roles-page">
    <div class="page-header">
      <h1>角色管理</h1>
      <div class="actions">
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>
          新增角色
        </el-button>
      </div>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <el-table :data="roleList" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="code" label="角色编码" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button size="small" type="info" @click="handlePermission(row)">
              权限
            </el-button>
            <el-button 
              size="small" 
              :type="row.status === 1 ? 'warning' : 'success'"
              @click="handleToggleStatus(row)"
            >
              {{ row.status === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button 
              size="small" 
              type="danger" 
              @click="handleDelete(row)"
              :disabled="row.code === 'super_admin'"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const loading = ref(false)

// 角色列表数据
const roleList = ref([
  {
    id: 1,
    name: '超级管理员',
    code: 'super_admin',
    description: '系统超级管理员，拥有所有权限',
    status: 1,
    createTime: '2024-01-01 10:00:00'
  },
  {
    id: 2,
    name: '管理员',
    code: 'admin',
    description: '系统管理员，拥有大部分权限',
    status: 1,
    createTime: '2024-01-01 10:00:00'
  },
  {
    id: 3,
    name: '编辑员',
    code: 'editor',
    description: '内容编辑员，拥有内容管理权限',
    status: 1,
    createTime: '2024-01-01 10:00:00'
  },
  {
    id: 4,
    name: '普通用户',
    code: 'user',
    description: '普通用户，只有基础权限',
    status: 1,
    createTime: '2024-01-01 10:00:00'
  }
])

// 获取角色列表
const getRoleList = async () => {
  loading.value = true
  try {
    // 模拟API调用
    setTimeout(() => {
      loading.value = false
    }, 500)
  } catch (error) {
    ElMessage.error('获取角色列表失败')
    loading.value = false
  }
}

// 新增角色
const handleAdd = () => {
  ElMessage.info('新增角色功能开发中')
}

// 编辑角色
const handleEdit = (row) => {
  ElMessage.info(`编辑角色: ${row.name}`)
}

// 权限设置
const handlePermission = (row) => {
  ElMessage.info(`设置角色权限: ${row.name}`)
}

// 切换角色状态
const handleToggleStatus = async (row) => {
  const action = row.status === 1 ? '禁用' : '启用'
  try {
    await ElMessageBox.confirm(`确认${action}角色 "${row.name}" 吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    row.status = row.status === 1 ? 0 : 1
    ElMessage.success(`${action}成功`)
  } catch {
    // 用户取消
  }
}

// 删除角色
const handleDelete = async (row) => {
  if (row.code === 'super_admin') {
    ElMessage.warning('超级管理员角色不能删除')
    return
  }
  
  try {
    await ElMessageBox.confirm(`确认删除角色 "${row.name}" 吗？`, '危险操作', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'error'
    })
    
    const index = roleList.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      roleList.value.splice(index, 1)
      ElMessage.success('删除成功')
    }
  } catch {
    // 用户取消
  }
}

onMounted(() => {
  getRoleList()
})
</script>

<style scoped>
.roles-page {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.table-container {
  margin-top: 20px;
}
</style>